package com.vanward.mes.scan.test;

import java.sql.*;

public class LotVerificationTest {
    
    public static void main(String[] args) {
        String lotNumber = "2511000030102104010000025";
        System.out.println("=== 批次编号验证测试 ===");
        System.out.println("验证批次: " + lotNumber);
        
        try {
            // 这里使用JDBC直接连接数据库进行验证
            String url = "jdbc:mysql://localhost:3306/mes?useSSL=false&serverTimezone=UTC";
            String user = "root";
            String password = "password";
            
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, password);
            
            String sql = "SELECT * FROM LOT WHERE Lot_No = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, lotNumber);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                System.out.println("\n✅ 验证成功: 批次存在于系统中");
                System.out.println("\n批次详情:");
                System.out.println("- 批次编号: " + rs.getString("Lot_No"));
                System.out.println("- 批次ID: " + rs.getInt("Lot_Id"));
                System.out.println("- 状态: " + rs.getString("Status"));
                System.out.println("- 开始时间: " + rs.getString("Start_Time"));
                System.out.println("- 结束时间: " + rs.getString("End_Time"));
                
                // 检查完工日志
                int lotId = rs.getInt("Lot_Id");
                checkLotFinishedLog(conn, lotId);
                
            } else {
                System.out.println("\n❌ 验证失败: 批次不存在于系统中");
            }
            
            rs.close();
            stmt.close();
            conn.close();
            
        } catch (Exception e) {
            System.out.println("\n❌ 验证过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void checkLotFinishedLog(Connection conn, int lotId) throws SQLException {
        String sql = "SELECT * FROM Lot_Finished_Log WHERE Lot_Id = ?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, lotId);
        ResultSet rs = stmt.executeQuery();
        
        if (rs.next()) {
            System.out.println("\n✅ 批次完工日志存在:");
            System.out.println("- 日志ID: " + rs.getInt("Id"));
            System.out.println("- 完工时间: " + rs.getString("Finished_Time"));
        } else {
            System.out.println("\n⚠️ 批次暂无完工日志记录");
        }
        
        rs.close();
        stmt.close();
    }
}